package com.taxi.service;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.taxi.constant.IdentityConstant;
import com.taxi.dto.OrderInfo;
import com.taxi.remote.ServiceOrderInfoClient;
import com.taxi.remote.ServiceSsePushClient;
import com.taxi.request.OrderRequest;
import com.taxi.request.PushRequest;
import com.taxi.result.Result;
import com.taxi.util.BigDecimalUtils;
import io.swagger.models.auth.In;
import net.sf.json.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * @Author: aniu
 * @Date: 2023/10/11
 * @ClassName: PayService
 * @Description: Write class functionality and purpose here.
 * @Version: 1.0
 */
@Service
public class PayService {
    @Autowired
    private ServiceSsePushClient serviceSsePushClient;
    @Autowired
    private ServiceOrderInfoClient serviceOrderInfoClient;

    /**
     * 司机推送支付信息，订单完成
     * @param
     * @return
     */
    public Result pushPayInfo(Long orderId, String price,Long passengerId) {
        //修改订单状态
        OrderRequest orderRequest = new OrderRequest();
        orderRequest.setOrderId(orderId);
        serviceOrderInfoClient.pushPayInfo(orderRequest);
        // 获取订单详情
        OrderInfo orderInfo = serviceOrderInfoClient.detail(orderId).getData();
        //推送订单消息给乘客
        JSONObject passengerContent = new JSONObject();
        passengerContent.put("orderId",orderInfo.getId());
        passengerContent.put("departure", orderInfo.getDeparture());
        passengerContent.put("destination", orderInfo.getDestination());
        passengerContent.put("price", price);
        passengerContent.put("driveTime", orderInfo.getDriveTime());
        passengerContent.put("driveMile", orderInfo.getDriveMile());
        passengerContent.put("orderStatus",orderInfo.getOrderStatus());
        passengerContent.put("currentLongitude",orderInfo.getDestLongitude());
        passengerContent.put("currentLatitude",orderInfo.getDestLatitude());

        PushRequest pushRequest = new PushRequest();
        pushRequest.setUserId(passengerId);
        pushRequest.setIdentity(IdentityConstant.PASSENGER_IDENTITY);
        pushRequest.setContent(passengerContent.toString());
        serviceSsePushClient.push(pushRequest);
        return Result.ok();
    }
}
